/******************************************************************************
 * Copyright (c) 2014 - 2016. Danlu.com Co.Ltd. All rights reserved.          *
 ******************************************************************************/
package cn.com.dhc.danlu.widget.holder;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.coupon.DealersCouponUsedActivity;
import cn.com.dhc.danlu.activity.delivery.order.MyOrdersActivity;
import cn.com.dhc.danlu.activity.envelope.DanluEnvelopeActivity;
import cn.com.dhc.danlu.activity.message.MessageContainerActivity;
import cn.com.dhc.danlu.activity.myself.AboutDanluActivity;
import cn.com.dhc.danlu.activity.myself.ChangePasswordActivity;
import cn.com.dhc.danlu.activity.myself.MyAccountActivity;
import cn.com.dhc.danlu.activity.myself.MyInvoiceActivity;
import cn.com.dhc.danlu.activity.myself.MyReceiveAddressActivity;
import cn.com.dhc.danlu.business.favorite.activity.FavoriteActivity;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.util.PreferenceUtils;
import cn.com.dhc.danlu.util.TipCountUtil;
import cn.com.dhc.danlu.widget.BadgeView;
import cn.com.dhc.danlu.widget.CustomItemLayout;
import cn.com.dhc.danlu.widget.couponview.filterview.FilterType;
import cn.com.dhc.danlu.widget.dialog.ViewDialog;
import cn.com.hd.mall.web.webservices.entity.response.order.OrderCountResponse;

import static cn.com.dhc.danlu.R.id.tv_sale_type_location;
import static cn.com.dhc.danlu.R.id.tv_terminal_name;
import static cn.com.dhc.danlu.dlware.global.DanluApplication.agent;

/**
 * @author: 吴海洋
 * @version: 1.0.0
 * @since: 2016-11-07 04:22
 * @mail: wuhaiyang@danlu.com
 * @desc: 我的丹露页面的所有控件
 */
public class MyselfHolder implements OnClickListener {
    // 终端店名称
    public TextView tvTerminalName;
    // 销售类型和地区
    public TextView tvSaleTypeLocation;
    // 显示用户账号
    public TextView tv_account;
    // 我的红包个数
    public TextView tv_danlu_envelop_count;
    // 我的红包个数
    public TextView tv_dealer_envelop_count;
    // 我的红包个数
    public TextView tv_dealer_coupon_count;
    // 我的账号 点击进入我的资料详细界面
    private RelativeLayout rl_my_account;
    // 丹露红包
    private LinearLayout ll_danlu_envelop;
    // 经销商红包
    private LinearLayout ll_dealer_envelop;
    // 经销商优惠券
    private LinearLayout ll_dealer_coupon;
    // 我的订单
    private CustomItemLayout mil_orders;
    // 待付款
    private LinearLayout ll_await_pay;
    // 待卖家 收货
    private LinearLayout ll_await_send;
    // 待收货
    private LinearLayout ll_await_receive;
    // 收货地址管理
    private CustomItemLayout mil_address;
    // 发票抬头管理
    private CustomItemLayout mil_invoice;
    // 我的银行卡
    private CustomItemLayout mil_bankcard;
    // 修改密码
    private CustomItemLayout mil_modifypwd;
    // 关于丹露
    private CustomItemLayout mil_about_danlu;
    // 客服电话
    private CustomItemLayout mil_service_tel;
    private CustomItemLayout mil_collect;
    // 退出
    private Button btnQuit;
    // 跳转到消息列表界面
    private ImageView iv_message_flag;
    // 待付款角标
    private BadgeView waitPayImgBg;
    // 待发货角标
    private BadgeView waitSendImgBg;
    // 待收货角标
    private BadgeView llytWaitReceiveBg;
    private BadgeView bv_msg_badge;
    private ImageView iv_wait_pay;
    private ImageView iv_wait_send;
    private ImageView iv_wait_receive;
    private OnEventTransfer mCallback;

    public MyselfHolder(View view) {
        tvTerminalName = (TextView) view.findViewById(tv_terminal_name);
        tvSaleTypeLocation = (TextView) view.findViewById(tv_sale_type_location);
        rl_my_account = (RelativeLayout) view.findViewById(R.id.rl_my_account_container);
        tv_account = (TextView) view.findViewById(R.id.tv_account);
        ll_danlu_envelop = (LinearLayout) view.findViewById(R.id.ll_danlu_envelop);
        ll_dealer_envelop = (LinearLayout) view.findViewById(R.id.ll_dealer_envelop);
        ll_dealer_coupon = (LinearLayout) view.findViewById(R.id.ll_dealer_coupon);
        tv_danlu_envelop_count = (TextView) view.findViewById(R.id.tv_danlu_envelop_count);
        tv_dealer_envelop_count = (TextView) view.findViewById(R.id.tv_dealer_envelop_count);
        tv_dealer_coupon_count = (TextView) view.findViewById(R.id.tv_dealer_coupon_count);
        mil_orders = (CustomItemLayout) view.findViewById(R.id.mil_orders);
        ll_await_pay = (LinearLayout) view.findViewById(R.id.ll_await_pay);
        ll_await_send = (LinearLayout) view.findViewById(R.id.ll_await_send);
        ll_await_receive = (LinearLayout) view.findViewById(R.id.ll_await_receive);
        mil_address = (CustomItemLayout) view.findViewById(R.id.mil_address);
        mil_invoice = (CustomItemLayout) view.findViewById(R.id.mil_invoice);
        mil_bankcard = (CustomItemLayout) view.findViewById(R.id.mil_bankcard);
        mil_modifypwd = (CustomItemLayout) view.findViewById(R.id.mil_modifypwd);
        mil_about_danlu = (CustomItemLayout) view.findViewById(R.id.mil_about_danlu);
        mil_service_tel = (CustomItemLayout) view.findViewById(R.id.mil_service_tel);
        mil_collect = (CustomItemLayout) view.findViewById(R.id.mil_collect);
        btnQuit = (Button) view.findViewById(R.id.btnQuit);
        iv_message_flag = (ImageView) view.findViewById(R.id.iv_message_flag);
        iv_wait_pay = (ImageView) view.findViewById(R.id.iv_wait_pay);
        iv_wait_send = (ImageView) view.findViewById(R.id.iv_wait_send);
        iv_wait_receive = (ImageView) view.findViewById(R.id.iv_wait_receive);
    }

    @Override
    public void onClick(View v) {
        Bundle data = null;
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        switch (v.getId()) {
            case R.id.iv_message_flag:
                // 跳转到消息中心界面
                intent.setClass(getContext(), MessageContainerActivity.class);
                getContext().startActivity(intent);
                break;

            case R.id.rl_my_account_container: // 我的账号
                intent.setClass(getContext(), MyAccountActivity.class);
                intent.putExtra("isForceActive", agent.getLoginUser().getSysParm().getActForceInd().equals("0"));
                intent.putExtra("isFromLogin", false);
                getContext().startActivity(intent);
                break;

            case R.id.ll_danlu_envelop:  // 丹露红包
                intent.setClass(getContext(), DanluEnvelopeActivity.class);
                getContext().startActivity(intent);
                break;

            case R.id.ll_dealer_envelop:
                // 跳转: 经销商红包
                DealersCouponUsedActivity.startSelf(getContext(), FilterType.dealers_envelop);
                break;

            case R.id.ll_dealer_coupon:
                // 跳转: 经销商优惠券
                DealersCouponUsedActivity.startSelf(getContext(), FilterType.dealers_coupon);
                break;

            case R.id.mil_orders: // 我的订单
                MyOrdersActivity.startSelf(getContext(), MyOrdersActivity.ALL_INDEX);
                break;

            case R.id.ll_await_pay: // 代付款
                MyOrdersActivity.startSelf(getContext(), MyOrdersActivity.AWAIT_PAY_INDEX);
                break;

            case R.id.ll_await_send: // 待卖家发货
                MyOrdersActivity.startSelf(getContext(), MyOrdersActivity.AWAIT_SEND_INDEX);
                break;

            case R.id.ll_await_receive: // 待收货
                MyOrdersActivity.startSelf(getContext(), MyOrdersActivity.AWAIT_RECEIVE_INDEX);
                break;

            case R.id.mil_address: // 收货地址管理
                intent.setClass(getContext(), MyReceiveAddressActivity.class);
                getContext().startActivity(intent);
                break;

            case R.id.mil_invoice:
                // 跳转到发票抬头
                intent.setClass(getContext(), MyInvoiceActivity.class);
                getContext().startActivity(intent);
                break;

            case R.id.mil_bankcard:  // 我的银行卡
                if (null != mCallback) {
                    mCallback.performClickBinkCard();
                }
                break;

            case R.id.mil_modifypwd: // 修改密码
                intent.setClass(getContext(), ChangePasswordActivity.class);
                getContext().startActivity(intent);
                break;

            case R.id.mil_about_danlu:
                // 跳转到关于丹露页面
                intent.setClass(getContext(), AboutDanluActivity.class);
                getContext().startActivity(intent);
                break;

            case R.id.mil_service_tel:
                // 16/9/23 合并进度审批需求代码后,此处使用CommonUtis中的抽出代码 lyf
                // 直接拨打电话 | 打开拨号界面 此处直接拨打电话
                new ViewDialog.Builder(getContext()).setMessage("400-011-6666").setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setPositiveButton(R.string.btn_confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        CommonUtils.callHelp((Activity) getContext());
                        dialog.dismiss();
                    }
                }).create().show();
                break;

            case R.id.btnQuit:
                // 创建退出对话框
                createQuitDialog();
                break;

            case R.id.mil_collect:
                //跳转到我的收藏画面
                intent.setClass(getContext(), FavoriteActivity.class);
                getContext().startActivity(intent);
                break;

            default:
                break;
        }
    }

    public Context getContext() {
        return tvTerminalName.getContext();
    }

    private void createQuitDialog() {
        // 创建Builder
        ViewDialog.Builder builder = new ViewDialog.Builder(getContext());
        // 设置标题
        builder.setTitle(R.string.prompt_label);
        // 设置信息
        builder.setMessage(R.string.logout_prompt_label);
        // 设置Positive按钮点击监听
        builder.setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 注销账号
                quit();
                // 对话框关闭
                dialog.dismiss();
            }
        });

        // 设置Negative按钮点击监听
        builder.setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 对话框关闭
                dialog.dismiss();
            }
        });
        // 创建对话框并显示
        builder.create().show();
    }

    protected void quit() {
        CommonUtils.exitCleanAction(getContext());
    }

    /**
     * 刷新消息角标
     */
    public void refreshMsgBadge(int count) {
        if (count > 0) {
            if (null == bv_msg_badge) {
                bv_msg_badge = new BadgeView(getContext(), iv_message_flag);
                bv_msg_badge.setBadgeMargin(0, 0);
                bv_msg_badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
                bv_msg_badge.setTextSize(10);
            }

            bv_msg_badge.setText(TipCountUtil.cover(count, TipCountUtil.SMALL_FLAG));
            bv_msg_badge.show();
        } else {
            if (bv_msg_badge != null) {
                bv_msg_badge.hide();
            }
        }
    }

    public void bindAction() {
        iv_message_flag.setOnClickListener(this);
        rl_my_account.setOnClickListener(this);
        ll_danlu_envelop.setOnClickListener(this);
        ll_dealer_envelop.setOnClickListener(this);
        ll_dealer_coupon.setOnClickListener(this);
        mil_orders.setOnClickListener(this);
        ll_await_pay.setOnClickListener(this);
        ll_await_send.setOnClickListener(this);
        ll_await_receive.setOnClickListener(this);
        mil_address.setOnClickListener(this);
        mil_invoice.setOnClickListener(this);
        mil_bankcard.setOnClickListener(this);
        mil_modifypwd.setOnClickListener(this);
        mil_about_danlu.setOnClickListener(this);
        mil_service_tel.setOnClickListener(this);
        mil_collect.setOnClickListener(this);
        btnQuit.setOnClickListener(this);
    }

    // 16/9/23 99+ 代码抽出 lyf
    public void refreshBadge(OrderCountResponse count) {
        // 待付款角标
        if (waitPayImgBg == null) {
            waitPayImgBg = new BadgeView(getContext(), iv_wait_pay);
        }
        setTips(waitPayImgBg, Integer.parseInt(count.getOrderCount().getPendingPaymentOrderCount()));

        // 待发货角标
        if (waitSendImgBg == null) {
            waitSendImgBg = new BadgeView(getContext(), iv_wait_send);
        }
        setTips(waitSendImgBg, Integer.parseInt(count.getOrderCount().getWaitingShipmentOrderCount()));

        // 待收货角标
        if (llytWaitReceiveBg == null) {
            llytWaitReceiveBg = new BadgeView(getContext(), iv_wait_receive);
        }
        setTips(llytWaitReceiveBg, Integer.parseInt(count.getOrderCount().getWaitingReceiveOrderCount()));
    }

    /**
     * 设置角标提示数字
     *
     * @param badgeView 需要设置角标数字的View
     * @param tipCount  提示数字
     */
    private void setTips(BadgeView badgeView, int tipCount) {
        badgeView.setTextSize(9);
        if (tipCount > 0) {
            badgeView.show();
            badgeView.setText(TipCountUtil.cover(tipCount, TipCountUtil.SMALL_FLAG));
        } else {
            badgeView.hide();
        }
    }

    public void setEventCallback(OnEventTransfer mCallback) {
        this.mCallback = mCallback;
    }

    public interface OnEventTransfer {
        void performClickBinkCard();

        PreferenceUtils getPrefrenceUtil();
    }
}
